package com.meixiang.activity.homes.shopping;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.request.BaseRequest;
import com.meixiang.R;
import com.meixiang.activity.account.manager.AddressManageActivity;
import com.meixiang.adapter.shopping.GoodsReceiptAddressAdapter;
import com.meixiang.entity.account.AddressListEntity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.tool.Tool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 选择收货地址
 *
 * @author LZRUI
 */
public class SelectGoodsReceiptAddressActivity extends BaseActivity implements OnItemClick {

    private static final String TAG = "SelectGoodsReceiptAddressActivity";

    @Bind(R.id.ry_address)
    RecyclerView ryAddress;
    private GoodsReceiptAddressAdapter addressAdapter;
    private List<AddressListEntity> addressList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_select_goods_receipt_address);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        HttpUtils.tagCancelRequest(TAG);
    }

    @Override
    protected void initTitle() {
        setRightTitle("选择收货地址", "管理", this);
    }

    @Override
    protected void initView() {
        ryAddress.setLayoutManager(new LinearLayoutManager(this));
        ryAddress.setAdapter(addressAdapter = new GoodsReceiptAddressAdapter());
        addressAdapter.setOnItemClick(this);
    }

    @Override
    protected void setData() {

    }



    @Override
    protected void onResume() {
        super.onResume();
        getAddressList();
    }



    @Override
    public void onClick(View v) {
        if (Tool.isFastDoubleClick()) return;
        // 管理按钮
        if (v.getId() == R.id.title_right) {
            startActivity(new Intent(this, AddressManageActivity.class));
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        // 返回到确认订单界面
        AddressListEntity entity = addressList.get(position);
        Intent intent = new Intent();
        intent.putExtra(VerifyGoodsOrderActivity.ADDRESS, entity);
        setResult(RESULT_OK, intent);
        finish();
    }

    private void getAddressList() {
        HttpUtils.post(Config.MY_ADDRESS_INDEX, TAG, null, new HttpCallBack(SelectGoodsReceiptAddressActivity.this) {

            @Override
            public void onBefore(BaseRequest request) {
                if (!Tool.isNetworkAvailable(activity)) {
                    OkHttpUtils.getInstance().cancelTag(SelectGoodsReceiptAddressActivity.this);
                    if (status == null) return;
                    status.showNoNewWork(null);
                } else {
                    if (status == null) return;
                    status.showLoading();
                }
            }

            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                try {
                    String json = result.getString("list");
                    addressList = new Gson().fromJson(json, new TypeToken<List<AddressListEntity>>() {
                    }.getType());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String code, String message) {

            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                if (status == null || addressAdapter == null) return;
                addressAdapter.refreshData(addressList);
                if (addressList == null || addressList.size() == 0) {
                    status.showNoData("暂无地址，请刷新试试", "立即刷新", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getAddressList();
                        }
                    });
                } else {
                    status.removeAttachView();
                }
            }
        });
    }
}
